<?php
/**
 * Created by xiaosong
 * E-mail:4155433@gmail.com
 * Date: 2019/10/9
 * Time: 14:10
 */

namespace App\Services\common;


use App\Models\goods\GoodsCate;
use App\Models\goods\Order;
use App\Models\nursery\Major;
use App\Models\templates\Delivery;
use App\Models\User;

class App
{
    public static function getUserInfo($field = '',$user_id,$cache = 3)
    {

    }

    /**
     * 生成uuid
     */
    public static function getUuid($user_id,$rand = 1 )
    {
        $uuid = 110000+$user_id*100+rand(10,99)+$rand;

        $has  = User::where('uuid',$uuid)->value('id');

        if ($has){
            $rand++;
            return self::getUuid($uuid,$rand);
        }
        return $uuid;

    }


    public static function majors()
    {
        return Major::select(['id','name'])->orderBy('id')->get();
    }


    public static function getDelivery():array
    {
        $array =  Delivery::select('id','name')
            ->orderBy('sort')
            ->get()
            ->toArray();

        $array = array_pluck($array, 'name','id');
        $array[0] = "免邮";
        ksort($array);
        return $array;
    }

    public static function printTicket(Order $order)
    {


        $config = new YlyConfig('1046181561','a956ffcc58ab64f599a767e73bf66244');
        $client = new YlyOauthClient($config);
        $token = $client->getToken();   //若是开放型应用请传授权码code

        $printer = new PrinterService($token->access_token, $config);
        $data = $printer->addPrinter('4004723335', '541235366052');

        $print = new PrintService($token->access_token, $config);
        $content = "<FS2><center>**牛视界**</center></FS2>";
        $content .= str_repeat('.', 32);
        $content .= "<FS2><center>--在线支付--</center></FS2>";
//        $content .= "<FS><center>张周兄弟烧烤</center></FS>";
        $content .= "订单时间:". $order->pay_at .$order->id. "\n";
        $content .= "订单编号:".$order->order_no."\n";
        $content .= str_repeat('*', 14) . "商品" . str_repeat("*", 14);
        $content .= "<table>";
        $content .= "<tr><td>商品名</td><td>数量</td><td>价格</td></tr>";
        foreach ($order->details as $detail){
            if (!empty($detail->goods->alias)){
                $name = $detail->goods->alias;
            }else{
                $name = $detail->goods_name;
            }
            if ($detail->spec){
                $name .= '-'.$detail->spec;
            }
            if ($detail->type_name){
                $name .= '-'.$detail->type_name;
            }
            $content .= sprintf("<tr><td>%s</td><td>%s</td><td>%s</td></tr>",$name,$detail->num,$detail->total_price);
        }

//        $content .= "<tr><td>烤土豆(超级辣)</td><td>x3</td><td>5.96</td></tr>";
//        $content .= "<tr><td>烤豆干(超级辣)</td><td>x2</td><td>3.88</td></tr>";
//        $content .= "<tr><td>烤鸡翅(超级辣)</td><td>x3</td><td>17.96</td></tr>";
//        $content .= "<tr><td>烤排骨(香辣)</td><td>x3</td><td>12.44</td></tr>";
//        $content .= "<tr><td>烤韭菜(超级辣)</td><td>x3</td><td>8.96</td></tr>";
        $content .= "</table>";
        $content .= str_repeat('.', 32);
//        $content .= "<QR>这是二维码内容</QR>";

        if ($order->is_safe){
            $order->old_price += 1;
        }

        $content .= "小计:￥".$order->old_price."\n";

        $content .= "折扣:￥".($order->old_price - $order->total_price)." \n";
        $content .= str_repeat('*', 32);
        $content .= "订单总价:￥".$order->total_price." \n";
        $content .= "<FS2><center>**完**</center></FS2>";
        $data = $print->index('4004723335',$content,'x');
        var_dump($data);
    }

}